<?php
/**
 * User: xzq
 * Date: 2018/8/8
 * Time: 15:43
 */

namespace app\library\admin;
use app\admin\controller\Admin;
use app\library\model\Reservation as ReservationModel;
use app\library\model\Area as AreaModel;
use app\library\model\User as UserModel;

class Reservation extends Admin
{
    public function index()
    {
        $this->assign('tab_type', 3);
        if ($this->request->isAjax()) {
            $where = $data = [];
            $page = input('param.page/d', 1);
            $limit = input('param.limit/d', 15);
            $card_id = input('param.card_id');
            $status = input('param.status');
            $name = input('param.name');
            $stime = input('param.stime');
            $etime = input('param.etime');
            if (!empty($card_id)){
                $where['card_id'] = $card_id;
            }
            if (!empty($name)){
                $where['name'] = $name;
            }
            if (!empty($status) || $status === "0"){
                $where['r.status'] = $status;
            }
            if (!empty($stime) && !empty($etime) && $stime >= $etime){
                return ['code'=>0,'count'=>0,'data'=>[],'msg'=>'开始不能大于结束时间'];
            }
            if(!empty($stime)){
                $stime = strtotime($stime);
                $where['startTime'] = ['>=',$stime];
            }
            if(!empty($etime)){
                $etime = strtotime($etime);
                $where['endTime'] = ['<=',$etime];
            }
            $area = new AreaModel();
            $user = new UserModel();
            $reservation = new ReservationModel();
            $list = ReservationModel::where($where)
                ->alias('r')
                ->field('r.id,card_id,name,areaid,seatNum,seatType,startTime,endTime,leaveTime,r.status,r.ctime')
                ->join('user u','r.userid = u.id')
                ->page($page)->limit($limit)->order('id desc')->select();
            $areaName = $area->getAllAreaName();
            foreach ($list as $key => $value) {
                foreach ($areaName as $k => $v) {
                    if ($value['areaid'] == $k) {
                        $list[$key]['areaname'] = $v;
                    }
                }
                switch ($value['seatType']) {
                    case '1': $seatTypeName = '硬座';break;
                    case '2': $seatTypeName = '软座';break;
                    case '3': $seatTypeName = '学生仓';break;
                    case '4': $seatTypeName = '交流间';break;
                    default:$seatTypeName = '暂无';; break;
                }
                $list[$key]['seatType'] = $seatTypeName;
                switch ($value['status']) {
                    case '0': $status = '待使用';break;
                    case '1': $status = '取消预约';break;
                    case '2': $status = '签到成功';break;
                    case '3': $status = '违规';break;
                    case '4': $status = '提前离场';break;
                    case '5': $status = '签退';break;
                    default:$status = '暂无';; break;
                }
                $list[$key]['status'] = $status;
                $list[$key]['startTime'] = date("Y-m-d H:i",$value['startTime']);
                $list[$key]['endTime'] = date("Y-m-d H:i",$value['endTime']);
                if (empty($value['leaveTime'])){
                    $list[$key]['leaveTime'] = '';
                }else{
                    $list[$key]['leaveTime'] = date("Y-m-d H:i",$value['leaveTime']);
                }
                $list[$key]['ctime'] = date("Y-m-d H:i",$value['ctime']);
            }
            $data['data'] = $list;
            $data['count'] = ReservationModel::where($where)->alias('r')->join('user u','r.userid = u.id')->count('r.id');
            $data['code'] = 0;
            $data['msg'] = '';
            return json($data);
        }
        return $this->fetch();
    }
}